<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_AjaxDuplicatePublicationAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
            $row = $this->getRequestParameter("row","");
            $id = is_array($row) ? $row[0] : $row;
            
            $originalPublication = $this->getDocumentProvider()->getByUniqueId($id, $this->getContextLang());
            $formValues = $this->getFormValues("form");
            $errors = array();

            if(count($formValues) > 0)
            {
                if($formValues['label'] == "")
                {
                    $errors["label"] = $this->getLocale('backoffice.errors.required');
                }
                else
                {
                    $parent = $this->getDocumentProvider()->getByUniqueId($formValues['folder']);
                    $errors["exist"] = $this->duplicatePublication($formValues, $originalPublication,$parent);

                    if(is_bool($errors["exist"]) && $errors["exist"] == true)
                    {
                        $this->addToModel("destPublication", $originalPublication);
                        $errors["status"] = "success";
                        $errors["dest"] = $parent->getEditUrl($parent->getLang());
                    }
                }

                $this->addToModel("errors", $errors);
            }

            if(!isset($errors['status']))
            {
                $values["label"]   = $originalPublication->getLabel()." ( ".$this->getLocale('backoffice.errors.copy')." )";
                $root = method_exists($originalPublication, "getWebsiteId") ? $originalPublication->getWebsiteId() : $originalPublication->getDocumentModuleName();
                $values["folders"] = m_backoffice_services_TreeService::getInstance()->getSelectArray($root, array("modules_backoffice/folder"), null, false, true, $originalPublication->getDocumentModuleName(), 'add');
                $values["folder"]  = $originalPublication->getDocumentParent()->getLabel();
                $this->addToModel("values", $values);
            }

            $this->addToModel("originalPublication", $originalPublication);

            $this->addToModel("destPublication", $originalPublication);
            $this->setView('success');
	}	

	private function duplicatePublication($formValues, $originalPub,$parent=null)
	{
        $destPublication = $originalPub->cloneToNew();
        if($parent==null)
        {
            $parent = $this->getParent($originalPub);
        }

        $destPublication->setPublicationStatus("DRAFT")->setAuthor($this->getUser()->getLogin())->setLabel($formValues['label']);
        $destPublication->save(false);

        $parent->addItem($destPublication)->save();
        return true;
	}

	private function getParent($document)
	{
		return f_relation_ManagerNew::getInstance($document)->getParents()->withLang($document->getLang())->retrieveDocuments();
	}
}